package com.sciai.utils;

/**
 * TODO lb <类名>
 *
 * @author tqx
 * @version 1.0.0.0
 * <p>Title: StringManipulation </p >
 * <p>Description: TODO tqx </p >
 * Package: com.sciai.utils
 * Company: 沪渝研究院
 * Copyright: Copyright(c) AI+X
 * Date: 2023年11月03日 23:19
 */
public class StringManipulation {
    public static String manipulateString(String input) {
        if (input.length() < 2) {
            // 如果长度小于2，返回第一个字符+1个*
            return input.substring(0, 1) + "*";
        } else {
            // 如果长度大于等于2，将中间2个字符替换为*
            int length = input.length();
            int middleStart = (length - 1) / 2;
            int middleEnd = length % 2 == 0 ? middleStart + 1 : middleStart;

            StringBuilder result = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i >= middleStart && i <= middleEnd) {
                    result.append("*");
                } else {
                    result.append(input.charAt(i));
                }
            }

            return result.toString();
        }
    }

    public static void main(String[] args) {
        String input1 = "abc";
        String input2 = "中文测试";
        String input3 = "12345";

        String output1 = manipulateString(input1);
        String output2 = manipulateString(input2);
        String output3 = manipulateString(input3);

        System.out.println(output1);  // 输出 "a*c"
        System.out.println(output2);  // 输出 "中*试"
        System.out.println(output3);  // 输出 "1*45"
    }
}